package com.itheima.anli;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/index.jsp")
public class LonginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        /*
        * 判断用户是否登陆：如何判断？？
        * 因为用户在登陆过后，需要将用户的对象存入session中，所以如果session中有用户对象证明登陆了
        * 如果没有证明用户没有登陆过*/
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        Object user = req.getSession().getAttribute("user");
        if (user==null){
            resp.sendRedirect(req.getContextPath()+"/login.jsp");
        }else {
            chain.doFilter(req,resp);
        }
    }
}
